package main

import (
	"fmt"
	"io/ioutil"
)

func main() {
	// openfile()

	fmt.Println(
		grade(0),
		grade(60),
		grade(70),
		grade(-3), //会中断程序执行
		grade(80))

}

// if
func openfile() {
	const filename = "basic/math.py"
	contents, err := ioutil.ReadFile(filename)
	if err != nil {
		fmt.Println(err)
	} else {
		fmt.Printf("%s\n", contents)
	}

	// 类似for,contents 生存期只在if的block
	if contents, err := ioutil.ReadFile("basic/branch.md"); err != nil {
		fmt.Println(err)
	} else {
		fmt.Printf("%s\n", contents)
	}
}

// switch
func grade(score int) string {
	g := ""
	switch {
	case score < 0 || score > 100:
		panic(fmt.Sprintf("Wrong score : %d", score))
	case score < 60:
		g = "F"
	case score < 80:
		g = "C"
	case score < 90:
		g = "B"
	default:
		panic(fmt.Sprintf("wrong score: %d", score))
	}
	return g
}
